package com.easy.api.utils;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.Map;

public class ObjectUtils {
    public ObjectUtils() {
    }

    public static Map<String, Object> toMap(Object obj) {
        if (obj == null) {
            return null;
        } else {
            Field[] fields = FieldUtils.getAllFields(obj.getClass());
            Map<String, Object> resultMap = new LinkedHashMap();
            Field[] var3 = fields;
            int var4 = fields.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                if (!Modifier.isStatic(field.getModifiers())) {
                    try {
                        resultMap.put(field.getName(), FieldUtils.readField(field, obj, true));
                    } catch (IllegalAccessException var8) {
                        var8.printStackTrace();
                    }
                }
            }

            return resultMap;
        }
    }

    public static Map<String, String> toMapStr(Object obj) {
        if (obj == null) {
            return null;
        } else {
            Field[] fields = FieldUtils.getAllFields(obj.getClass());
            Map<String, String> resultMap = new LinkedHashMap();
            Field[] var3 = fields;
            int var4 = fields.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                if (!Modifier.isStatic(field.getModifiers())) {
                    try {
                        resultMap.put(field.getName(), String.valueOf(FieldUtils.readField(field, obj, true)));
                    } catch (IllegalAccessException var8) {
                        var8.printStackTrace();
                    }
                }
            }

            return resultMap;
        }
    }

    public static Map<String, Object> toMapIgnoreNull(Object obj) {
        if (obj == null) {
            return null;
        } else {
            Field[] fields = FieldUtils.getAllFields(obj.getClass());
            Map<String, Object> resultMap = new LinkedHashMap();
            Field[] var3 = fields;
            int var4 = fields.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                if (!Modifier.isStatic(field.getModifiers())) {
                    try {
                        Object value = FieldUtils.readField(field, obj, true);
                        if (value != null) {
                            resultMap.put(field.getName(), value);
                        }
                    } catch (IllegalAccessException var8) {
                        var8.printStackTrace();
                    }
                }
            }

            return resultMap;
        }
    }

    public static Map<String, String> toMapIgnoreNullStr(Object obj) {
        if (obj == null) {
            return null;
        } else {
            Field[] fields = FieldUtils.getAllFields(obj.getClass());
            Map<String, String> resultMap = new LinkedHashMap();
            Field[] var3 = fields;
            int var4 = fields.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                if (!Modifier.isStatic(field.getModifiers())) {
                    try {
                        Object value = FieldUtils.readField(field, obj, true);
                        if (value != null) {
                            resultMap.put(field.getName(), String.valueOf(value));
                        }
                    } catch (IllegalAccessException var8) {
                        var8.printStackTrace();
                    }
                }
            }

            return resultMap;
        }
    }
}

